package de.recipeminer.ui.parsing;

import joptsimple.OptionException;
import joptsimple.ValueConverter;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Collections;
import java.util.regex.Pattern;

/**
 * @autor: Markus Ackermann
 */
public class OptionParser extends joptsimple.OptionParser {
    private static Logger logger = Logger.getLogger(OptionParser.class);

    public String getHelpText() {
        StringWriter stringWriter = new StringWriter(200);
        try {
            printHelpOn(stringWriter);
        } catch (IOException e) {
            throw new RuntimeException("Error reading paser help to string:", e);
        }
        return stringWriter.toString();
    }

    public static ValueConverter<Pattern> REGEXP_CONVERTER = new ValueConverter<Pattern>() {
        @Override
        public Pattern convert(String value) {
            return Pattern.compile(value);
        }

        @Override
        public Class<Pattern> valueType() {
            return Pattern.class;
        }

        @Override
        public String valuePattern() {
            return ".*";
        }
    };

    public static class CommandNameUnknownException extends OptionException {
        private String name;

        public CommandNameUnknownException(String name) {
            super(Collections.<String>emptyList());
            this.name = name;
        }

        @Override
        public String getMessage() {
            return name + " is not an available command";
        }
    }


}
